﻿using Dapper;
using Entity;
using IRepository;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Repository
{
    public class OrderDetailRepository : IOrderDetailRepository
    {
        private readonly IDbConnection _dbConnection;
        private readonly ILogger<OrderDetailRepository> _logger;

        public OrderDetailRepository(IDbConnection dbConnection, ILogger<OrderDetailRepository> logger)
        {
            _dbConnection = dbConnection;
            _logger = logger;
        }

        public async Task<PurchaseOrderDetailEntity> GetByPurchaseIdAndProductIdAsync(int purchaseId, int productId, IDbTransaction transaction)
        {
            // 修正SQL列名为数据库实际的Product_ID
            var sql = "SELECT * FROM wms_order_details WHERE purchase_id = @PurchaseId AND Product_ID = @ProductId AND is_delete = '0'";
            return await _dbConnection.QueryFirstOrDefaultAsync<PurchaseOrderDetailEntity>(
                sql,
                new { PurchaseId = purchaseId, ProductId = productId }, // 参数改为货品ID（int类型）
                transaction
            );
        }

        public async Task UpdateStkInNumAsync(PurchaseOrderDetailEntity entity, IDbTransaction transaction)
        {
            var sql = "UPDATE wms_order_details SET stkIn_num = @stkIn_num, update_time = GETDATE() WHERE purchase_details_id = @purchase_details_id";
            await _dbConnection.ExecuteAsync(sql, entity, transaction);
        }
    }
}
